package com.bfly.mall.product.service.impl;

import com.bfly.core.base.service.BaseServiceImpl;
import com.bfly.core.enums.SysError;
import com.bfly.core.exception.ServiceResponseException;
import com.bfly.mall.product.dao.ProductMapper;
import com.bfly.mall.product.entity.Product;
import com.bfly.mall.product.dto.ProductFormDTO;
import com.bfly.mall.product.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author 长沙商脉科技有限公司
 * @date 2020/9/21 12:32
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product, Integer> implements IProductService {

    @Autowired
    private IProductTxtService txtService;
    @Autowired
    private IProductImgService imgService;
    @Autowired
    private IProductAttrService attrService;
    @Autowired
    private IProductSpeAttrService speAttrService;
    @Autowired
    private IProductSkuService skuService;
    @Autowired
    private ProductMapper productMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void editShelf(int productId, boolean isOnShelf) {
        Product product = get(productId);
        if (product == null) {
            throw new ServiceResponseException(SysError.PARAM_ERROR, "不存在的产品信息!");
        }
        product.setOnShelf(isOnShelf);
        super.edit(product);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(ProductFormDTO formDTO) {
        Product product = new Product();

        BeanUtils.copyProperties(formDTO, product);

        product.setActualSales(0);
        product.setCreateDate(new Date());
        product.setUpdateDate(product.getCreateDate());
        super.save(product);

        mergeProductInfo(product.getId(), formDTO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void edit(ProductFormDTO formDTO) {
        Product product = get(formDTO.getId());
        if (product == null) {
            throw new ServiceResponseException(SysError.PARAM_ERROR, "不存在的产品信息!");
        }

        BeanUtils.copyProperties(formDTO, product);

        product.setUpdateDate(new Date());
        super.edit(product);

        mergeProductInfo(product.getId(), formDTO);
    }

    /**
     * 保存产品其他信息
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/9/24 20:25
     */
    private void mergeProductInfo(int productId, ProductFormDTO formDTO) {
        txtService.merge(productId, formDTO.getTxt());
        imgService.merge(productId, formDTO.getImgs());
        attrService.merge(productId, formDTO.getAttrs());
        speAttrService.merge(productId, formDTO.getSpeAttrs());
        skuService.merge(productId, formDTO.getSkus());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void incrementProductActualSales(int productId, int setup) {
        productMapper.incrementProductActualSales(productId, setup);
    }

    @Override
    public List<Product> getProductFavorite(int userId) {
        return productMapper.getProductFavorite(userId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void sortProduct(int productId, int seq) {
        productMapper.sortProduct(productId,seq);
        getCache().clear();
    }
}
